package cn.net.autocode.platform.table.ddl.mysql;


import cn.hutool.core.util.StrUtil;
import cn.net.autocode.platform.table.ddl.DataBaseDDL;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.dbManager.Dao;


public class MySqlDataBaseDDL implements DataBaseDDL {

    private final Dao dao;

    public MySqlDataBaseDDL(Dao dao){
        this.dao = dao;
    }

    public void createTable(String tableName) {

        String sql= "create table "+tableName+
                " (id varchar(36) not null primary key," +
                "sort_no int default 0," +
                "remark varchar(255))";
        dao.execute(sql);
    }

    public void createServTable(String tableName) {
        String sql = "create table "+tableName+
                " (id nvarchar(36) not null primary key)";
        dao.execute(sql);
    }

    public void addColumn(JSONObject column, String tableName) {
        String type = column.getString("type").toUpperCase();
        column.getString("name");
        String sql = "alter table "+tableName+" add column "+column.getString("name") + " "+type;

        sql = switch (type) {
            case "CHAR", "TIMESTAMP", "VARCHAR" -> sql + "(" + column.getIntValue("length") + ") ";
            case "DECIMAL" -> sql + "(" + column.getIntValue("length") + "," + column.getIntValue("scale") + ") ";
            case "INT", "TEXT","LONGTEXT", "DATETIME", "LONGBLOB" ->sql + " ";
            default -> throw new RuntimeException("未定义的类型："+type);
        };

        //设置长度

        //设置默认值
        String defaultvalue = column.getString("defaultvalue");
        if(StrUtil.isNotBlank(defaultvalue)&&!defaultvalue.startsWith("@")){
            sql = switch (type) {
                case "CHAR", "VARCHAR", "TEXT","LONGTEXT" -> sql + " default '" + defaultvalue + "'";
                case "TIMESTAMP", "DECIMAL", "INT", "DATETIME" -> sql + " default " + defaultvalue;
                default -> sql + " ";
            };

        }
        //非空约束
        String isnullable = column.getString("isnullable");
        if(StrUtil.isBlank(defaultvalue)&&"N".equals(isnullable)){
            sql = sql + " not null";
        }

        //设置字段说明
        String remarks = column.getString("remarks");
        if(StrUtil.isNotBlank(remarks)){
            sql = sql +" comment '"+remarks+"'";
        }
        dao.execute(sql);
    }

   
    public void modifyColumn(JSONObject column, String tableName) {
        String type = column.getString("type");
        String sql = "alter table "+tableName+" modify "+column.getString("name") + " "+type;
        sql = switch (type) {
            case "CHAR", "timestamp", "VARCHAR" -> sql + "(" + column.getIntValue("length") + ") ";
            case "DECIMAL" -> sql + "(" + column.getIntValue("length") + "," + column.getIntValue("scale") + ") ";
            case "INT", "LONGTEXT", "DATETIME", "LONGBLOB" -> sql + " ";
            default -> sql + " ";
        };

        //设置长度

        //设置默认值及非空约束
        String isnullable = column.getString("isnullable");
        String defaultvalue = column.getString("defaultvalue");
        if(StrUtil.isNotBlank(defaultvalue)&&!defaultvalue.startsWith("@")){
            sql = switch (type) {
                case "CHAR", "VARCHAR", "LONGTEXT" -> sql + " default '" + defaultvalue + "'";
                case "timestamp", "DECIMAL", "INT", "DATETIME" -> sql + " default " + defaultvalue;
                default -> sql + " ";
            };
        }

        /*if((StrUtil.isBlank(defaultvalue)||defaultvalue.startsWith("@"))&&"N".equals(isnullable)){
            sql = sql + " not null";
        }*/

        if("N".equals(isnullable)){
            sql = sql + " not null";
        }

        //设置字段说明
        String remarks = column.getString("remarks");
        if(StrUtil.isNotBlank(remarks)){
            sql = sql +" comment '"+remarks+"'";
        }

        dao.execute(sql);
    }

   
    public void delColumn(String column, String tableName) {
        String sql = "ALTER TABLE "+tableName+" DROP column "+column;
        dao.execute(sql);
    }


}
